package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.view.ui.activity.manage.CompanyTradesActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CompanyTradesModel extends BaseViewModel {

    CompanyTradesActivity companyTradesActivity;
    private ManageRepertory repertory;
    IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "","");

    public CompanyTradesModel(CompanyTradesActivity companyTradesActivity,ManageRepertory repertory) {
        this.companyTradesActivity = companyTradesActivity;
        this.repertory = repertory;
    }

    public void getCompanyTradesList(Map<String,Object> map){
        DialogHelper.showProgressDialog(companyTradesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getCompanyTradesList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> call, Response<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    companyTradesActivity.getCompanyTradesList(response.body().getData().getList());
                } else {
                    MToast.showToast(companyTradesActivity.getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void queryIndustryCategry(boolean isList) {
        DialogHelper.showProgressDialog(companyTradesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTradeTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<IpPickSelectEntity> list = new ArrayList<>();
                    list.addAll(response.body().getData());
                    IpPickSelectEntity entity2 = new IpPickSelectEntity("", "全部", "", "", "", "","");
                    list.add(0, entity2);
                    companyTradesActivity.tabData(list,isList);
                } else {
                    MToast.showToast(companyTradesActivity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void clickIndustry(boolean isList){
        queryIndustryCategry(isList);
    }

    public void dynamicList() {
        DialogHelper.showProgressDialog(companyTradesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getDynamicList().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<IpPickSelectEntity> list = new ArrayList<>();
                    IpPickSelectEntity entity2 = new IpPickSelectEntity("", "不限", "", "", "", "","");
                    list.add(0, entity2);
                    list.addAll(response.body().getData());
                    companyTradesActivity.showBrandPopWindow(list);
                } else {
                    MToast.showToast(companyTradesActivity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData() {
        DialogHelper.showProgressDialog(companyTradesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    companyTradesActivity.setCityData(response.body().getData(),repertory.getAreaCode());
                } else {
                    MToast.showToast(companyTradesActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
